package com.elvin.platform.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.elvin.platform.beans.AlarmRule;
import com.elvin.platform.beans.AlarmRuleInfo;
import com.elvin.platform.beans.AlarmRuleList;
import com.elvin.platform.service.AlarmRuleService;
import com.elvin.platform.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报警规则表 前端控制器
 * </p>
 *
 * @author zhenchl
 * @since 2022-04-26
 */
@Controller
@RestController
@Log4j2
@Api("报警规则管理")
@RequestMapping("/alarmRuleController")
public class AlarmRuleController {

  @Resource
  private AlarmRuleService service;

  @ApiOperation(value = "添加信息")
  @PostMapping("/addAlarmRules")
  public Result<Object> addAlarmRules(@RequestBody AlarmRuleInfo entity) {
    Result<Object> result = new Result<>();
    try {
      List<AlarmRuleList> alarmRuleLists = entity.getAlarmRuleLists();
      AlarmRule alarmRule = new AlarmRule();
      alarmRule.setUserId(entity.getUserId());
      alarmRule.setStatus(entity.getStatus());
      alarmRule.setOpenid(entity.getOpenid());
      alarmRule.setPhone(entity.getPhone());
      alarmRule.setRuleType(entity.getRuleType());
      alarmRule.setReceiveMsg(true);
      if (alarmRuleLists.size() > 0) {
        //新增前根据条件查询是否有值，有值则不能新增，无值则新增
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", entity.getUserId());
        for (AlarmRuleList alarmRuleList : alarmRuleLists) {
          map.put("dev_id", alarmRuleList.getDevId());
          List<AlarmRule> alarmRules = service.selectByMap(map);
          if (alarmRules.size() > 0) {
            continue;
          } else {
            alarmRule.setImei(alarmRuleList.getImei());
            alarmRule.setDeviceId(alarmRuleList.getDeviceId());
            alarmRule.setDevId(alarmRuleList.getDevId());
            service.insert(alarmRule);
          }
        }
      }
      result.success(entity, "新增成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


  @ApiOperation(value = "按条件添加")
  @PostMapping("/addByCondition")
  public Result<Object> addByCondition(@RequestBody AlarmRule entity) {
    Result<Object> result = new Result<>();
    try {
      //新增前根据条件查询是否有值，有值则不能新增，无值则新增
      Map<String, Object> map = new HashMap<>();
      map.put("user_id", entity.getUserId());
      map.put("dev_id", entity.getDevId());
      List<AlarmRule> alarmRules = service.selectByMap(map);
      if (alarmRules.size() > 0) {
        result.fail(null, "根据userId和devId获取到数据，不能添加");
      } else {
        service.insert(entity);
        result.success(entity, "新增成功");
      }
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


  @ApiOperation(value = "删除报警规则数据")
  @PostMapping("/delete")
  public Result<Object> delete(@ApiParam(name = "id", value = "报警规则id", required = true) @RequestParam("id") Long id) {
    Result<Object> result = new Result<>();
    try {
      service.deleteById(id);
      result.success("删除成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


  @ApiOperation(value = "批量删除报警规则数据")
  @PostMapping("/deleteBatch")
  public Result<Object> deleteBatch(@ApiParam(name = "id", value = "报警规则id", required = true) @RequestParam("id") String id) {
    Result<Object> result = new Result<>();
    try {
      String[] split = id.split(",");
      for (String s : split) {
        service.deleteById(Long.valueOf(s));
      }
      result.success("删除成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "根据ID获取报警规则数据")
  @PostMapping("/getById")
  public Result<Object> getById(@ApiParam(name = "id", value = "报警规则id", required = true) @RequestParam("id") Long id) {
    Result<Object> result = new Result<>();
    try {
      AlarmRule entity = new AlarmRule();
      entity.setId(id);
      entity = service.selectById(entity);
      result.success(entity);
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


  @ApiOperation(value = "新增")
  @PostMapping("/insert")
  public Result<Object> insert(@RequestBody AlarmRule entity) {
    Result<Object> result = new Result<>();
    try {
      service.insert(entity);
      result.success(entity, "新增成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


  @ApiIgnore
  @ApiOperation(value = "获取所有报警规则数据")
  @PostMapping("/queryAll")
  public Result<Object> queryAll() {
    Result<Object> result = new Result<>();
    try {
      List<AlarmRule> selectList = service.selectList(null);
      result.success(selectList);
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


  @ApiOperation(value = "分页获取所有数据")
  @PostMapping("/selectPage")
  public Result<Object> selectForPage(
      @ApiParam("用户id") @RequestParam(value = "userId", required = false) Long userId,
      @ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
      @ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false) Integer pageSize
  ) {
    Result<Object> result = new Result<>();
    try {
      EntityWrapper<AlarmRule> wrapper = new EntityWrapper<>();
      if (null != userId) {
        wrapper.eq("user_id", userId);
      }
      int count = service.selectCount(wrapper);
      if (count > 0) {
        List<AlarmRule> selectList = new ArrayList<>();
        if (pageNumber != null && pageSize != null) {
          Page<AlarmRule> page = new Page<>(pageNumber, pageSize);
          service.selectPage(page, wrapper);
          selectList = page.getRecords();
          result.success(selectList);
        } else {
          selectList = service.selectList(wrapper);
        }
        result.success(selectList, count);
      } else {
        result.success(null, count);
      }

    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


  @ApiOperation(value = "修改报警规则数据")
  @PostMapping("/update")
  public Result<Object> update(@RequestBody AlarmRule entity) {
    Result<Object> result = new Result<>();
    try {
      service.updateById(entity);
      result.success("修改成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


}
